//
//  RefreshView.swift
//  Refresh
//
//  Created by miaozhihui on 2017/3/7.
//  Copyright © 2017年 DeKuTree. All rights reserved.
//

import UIKit

/// 刷新视图 - 负责刷新相关的 UI 显示和动画
class RefreshView: UIView {
    
    /// 刷新状态
    var refreshState: RefreshState = .Normal {
        didSet {
            switch refreshState {
            case .Normal:
                tipLabel?.text = "继续使劲拉"
                UIView.animate(withDuration: 0.25, animations: {
                    self.tipIcon?.transform = CGAffineTransform.identity
                })
                // 恢复状态
                tipIcon?.isHidden = false
                indicator?.stopAnimating()
            case .Pulling:
                tipLabel?.text = "放手就刷新"
                UIView.animate(withDuration: 0.25, animations: { 
                    self.tipIcon?.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI+0.001))
                })
            case .Refreshing:
                tipLabel?.text = "正在刷新..."
                // 隐藏提示图标
                tipIcon?.isHidden = true
                // 显示指示器
                indicator?.startAnimating()
            }
        }
    }
    
    
    /// 提示图标
    @IBOutlet weak var tipIcon: UIImageView?

    /// 提示文本
    @IBOutlet weak var tipLabel: UILabel?

    /// 指示器
    @IBOutlet weak var indicator: UIActivityIndicatorView?
    
    /// 父视图高度
    var parentViewHeight: CGFloat = 0
    
    
    /// 实例化刷新视图
    ///
    /// - Returns: 刷新视图
    class func refreshView() -> RefreshView {
        
        let nib = UINib(nibName: "MTRefreshView", bundle: nil)
        
        return nib.instantiate(withOwner: nil, options: nil)[0] as! RefreshView
        
    }
}
